package com.sad.jetpack.v1.thememanager.api;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;

public class ThemeManager {

    public static IThemeAttrDynamicPerformer dynamicConfigCurrentInsideTheme(Context context){
        IThemeAttrDynamicPerformer performer=new DefaultThemeAttrDynamicPerformer(context);
        return performer;
    }
    public static LocalTheme setActivityThemeByLastLocalTheme(Activity activity){
        LocalTheme localTheme=LocalTheme.getLastLocalTheme(activity.getApplicationContext());
        if (activity!=null && !activity.isFinishing()){
            int currThemeResId = BasicThemeDesHelper.findResIdByLocalTheme(localTheme);
            if (-1!=currThemeResId){
                activity.setTheme(currThemeResId);
            }
        }
        return localTheme;
    }

    public static void setActivityThemeByDesCode(Activity activity, int code){
        int resId= BasicThemeDesHelper.findResIdByBasicThemeDesCode(code);
        if (activity!=null && !activity.isFinishing()){
            activity.setTheme(resId);
        }
    }

    public static void init(Application application,LocalTheme defaultLocalTheme,@StyleRes int defaultThemeResId){
        init(application,defaultLocalTheme,defaultThemeResId,null);
    }

    public interface InitActivityLifecycleCallback{
        void onActivityCreated(@NonNull Activity activity,@Nullable Bundle savedInstanceState);
    }

    public static void init(Application application,LocalTheme localTheme,@StyleRes int defaultThemeResId, InitActivityLifecycleCallback callback){
        BasicThemeDesHelper.initInsideBasicTheme(application);
        if (localTheme!=null){
            localTheme.setDefault(true);
            BasicThemeDes b=localTheme.getBasicThemeDes();
            if (b!=null){
                addExternalBasicThemeRes(b,defaultThemeResId);
                LocalTheme.DEFAULT_LOCAL_THEME=localTheme;
            }
        }

        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            @Override
            public void onActivityPreCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                if (callback!=null){
                    callback.onActivityCreated(activity,savedInstanceState);
                }
                else {
                    ThemeManager.setActivityThemeByLastLocalTheme(activity);
                }
            }

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    public static void addExternalBasicThemeRes(BasicThemeDes des, @StyleRes int newThemeResId){
        BasicThemeDesHelper.addBasicThemeDesRes(des,newThemeResId);
    }
    public static boolean isCurrNightMode(Context context) {
        try {
            return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
